package yunquan.wang.modules.customer.web;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import yunquan.wang.base.web.PageSearch;
import yunquan.wang.base.web.ShowMessage;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.ajax.AjaxUtils;
import yunquan.wang.modules.customer.entity.Contacts;
import yunquan.wang.modules.customer.service.ContactsService;
/**
 * 
 *  客户联系人。
 * 
 * @author wangyunquan
 * @mail wangyunquan@live.com QQ:1371392495
 *  @version 0.1
 */
@Controller
@RequestMapping("/customer/contancts")
public class ContactsController {

	@Resource
	private ContactsService service;
   /**
   * 列表查看
   * @param pageSearch 封转的页面的分页及搜索条件
   * @param model 返回的数据
   * @return
   */
	@RequestMapping(value = "/list.do")
	public String list(PageSearch pageSearch, Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,service.findPage(pageSearch.getPage(), pageSearch.getFilters()));
		return "customer/contancts/list";
	}
   /**
    *  保存或更新方法
    * @param entity 页面传入参数封装
    * @param bindingResult 参数绑定结果
    * @param redirectAttributes 跳转后需保留的信息
    * @param pageSearch 上一页面分页和搜索信息，用以跳转或返回的时候使用
    * @param model 返回数据
    * @return
    */
	@RequestMapping(value = "/save.do")
	public String save(@Valid Contacts entity, BindingResult bindingResult,
			RedirectAttributes redirectAttributes, PageSearch pageSearch,Model model) {
		if (bindingResult.hasErrors()) {
			model.addAttribute(WebConstants.FORMERROR,	AjaxUtils.bindErroJson(bindingResult));
			return "customer/contancts/input";
		} else {
			service.save(entity);
			redirectAttributes.addFlashAttribute(WebConstants.SHOW_MESSAGE,new ShowMessage(WebConstants.MESSAGE_SUCCESS,"action.save.success"));
		}
		return "redirect:./list.do";
	}
  /**
   *  查看
   * @param id 唯一标示
   * @param pageSearch 上一页面分页和搜索信息，用以跳转或返回的时候使用
   * @param model 返回数据
   * @return
   */
	@RequestMapping(value = "/view.do")
	public String view(String id, PageSearch pageSearch, Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, service.get(id));
		return "customer/contancts/view";
	}
 /**
  * 输入或更新方法
  * @param id 唯一标示
  * @param pageSearch  上一页面分页和搜索信息
  * @param model 返回数据
  * @return
  */
	@RequestMapping(value = "/input.do")
	public String input(String id, PageSearch pageSearch, Model model) {
		Contacts entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new Contacts();
			//默认为使用状态
			entity.setStatus(true);
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "customer/contancts/input";
	}
/**
 * 删除方法
 * @param id 标示列表
 * @param pageSearch 上一页面分页和搜索信息
 * @param redirectAttributes 跳转需保存的数据
 * @return
 */
	@RequestMapping(value = "/delete.do")
	public String delete(String[] id, PageSearch pageSearch,
			RedirectAttributes redirectAttributes) {
		for (String ids : id) {
			// service.delete(ids);
		}
		redirectAttributes.addFlashAttribute(WebConstants.SHOW_MESSAGE,new ShowMessage(WebConstants.MESSAGE_SUCCESS,"action.delete.success"));
		return "redirect:./list.do";
	}
}
